package study

import "fmt"

type DivideError struct {
	Dividend int
	Divisor  int
}

func (de *DivideError) Error() string {
	strFmt := "除数不能为0. divider: %d, dividor: %d"
	return fmt.Sprintf(strFmt, de.Dividend, de.Divisor)
}

func DivideErrorfunc(a, b int) (int, error) {
	if b == 0 {
		return 0, &DivideError{a, b}
	}
	return a / b, nil
}

func TestError() {
	_, err := DivideErrorfunc(10, 0)
	if err != nil {
		fmt.Println(err) // 输出：cannot divide 10 by 0
	}
}
